package com.xiaoma.registrar;

import java.lang.annotation.Annotation;
import java.util.Set;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;

/**
 * 扫描器
 * @author maj
 * @ClassName MyClassPathBeanDefinitionScanner
 * @Description
 * @date 2020-09-26 16:44
 */
public class MyClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

  public MyClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
    super(registry, useDefaultFilters);
  }

  /**
   * @addIncludeFilter 将自定义的注解添加到扫描任务中
   * @addExcludeFilter 将带有自定义注解的类 ，不加载到容器中
   */
  protected void registerFilters (Class<? extends Annotation> annotationType) {
    /**
     *  TODO addIncludeFilter  满足任意includeFilters会被加载
     */
    addIncludeFilter(new AnnotationTypeFilter(annotationType));
    /**
     *  TODO addExcludeFilter 同样的满足任意excludeFilters不会被加载
     */
    // addExcludeFilter(new AnnotationTypeFilter(MyService.class));
  }

  /**
   * 重写类扫描包路径加载器，调用父类受保护的扫描方法 doScan
   * @param basePackages
   * @return
   */
  @Override
  protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
    return super.doScan(basePackages);
  }

}
